<template>
  <div class="edit-container">
    <el-col :span="12">
      <el-form :model="dataForm" label-width="120px" :rules="dataFormRules" ref="dataForm" :size="size" label-position="right">
        <el-form-item label="用户名" prop="name">
          <el-input ref="name" v-model="dataForm.name" auto-complete="off" />
        </el-form-item>
        <el-form-item label="账号" prop="account">
          <el-input v-model="dataForm.account" auto-complete="off" :disabled="dataForm.id!==0" />
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="dataForm.password" type="password" auto-complete="off" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="dataForm.status">
            <el-radio v-for="item in statusList" :key="item.value" :label="item.value">{{item.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="角色" prop="roleIds">
          <el-select v-model="dataForm.roleIds" multiple placeholder="角色">
            <el-option v-for="item in roleList" :key="item.id" :label="item.name" :value="item.id" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button :size="size" @click.native="$router.go(-1)">取消</el-button>
          <el-button :size="size" type="primary" @click.native="save" :loading="loading">保存</el-button>
        </el-form-item>
      </el-form>
    </el-col>
  </div>
</template>

<script>
import { mapState } from 'vuex'
export default {
  data () {
    return {
      size: 'mini',
      loading: false,
      roleList: [],
      dataFormRules: {
        account: [
          { required: true, message: '请输入账号', trigger: 'blur' }
        ],
        name: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ]
      },
      // 新增编辑界面数据
      dataForm: {
        id: 0,
        name: '',
        password: this.initPassword,
        status: '0',
        roleIds: []
      }
    }
  },
  methods: {
    // 加载角色列表
    loadRoleList () {
      this.$api.role.getAll().then(res => {
        if (res.code === 0) {
          this.roleList = res.data
        }
      })
    },
    loadData () {
      if (this.$route.query.id !== undefined) {
        this.dataForm.id = this.$route.query.id
        this.$api.user.get({ id: this.dataForm.id }).then((res) => {
          this.dataForm = res.data
        })
      }
    },
    save () {
      this.$refs.dataForm.validate(valid => {
        if (valid) {
          this.loading = true
          let params = Object.assign({}, this.dataForm)
          if (this.dataForm.roleIds.length === 0) {
            this.dataForm.roleIds.push(-1)
          }
          this.$api.user.save(params).then((res) => {
            this.loading = false
            if (res.code === 0) {
              this.$message({ message: '操作成功', type: 'success' })
              this.$router.push(this.getCurrentParentPath() + '/list')
            } else {
              this.$message({ message: '操作失败, ' + res.message, type: 'error' })
            }
          })
        }
      })
    }
  },
  mounted () {
    this.loadRoleList()
    this.loadData()
    this.$refs.name.focus()
  },
  computed: {
    ...mapState({
      statusList: state => state.app.typeList.sys_normal_disable
    })
  }
}
</script>
